package com.cvte.boe.media;

import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

import com.cvte.boe.media.broadcast.MountActionReceiver;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wuguitong on 2018/4/9.
 */
public class MediaApplication extends Application{
    private static Context mContext;
    public static List<MountActionListener> mListeners;
    private static MountActionReceiver mReceiver;

    @Override
    public void onCreate() {
        super.onCreate();
        mContext = this;
        mListeners = new ArrayList<>();
        mReceiver = new MountActionReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_MEDIA_MOUNTED);
        filter.addAction(Intent.ACTION_MEDIA_EJECT);
        filter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
        filter.addAction(Intent.ACTION_MEDIA_REMOVED);
        filter.addDataScheme("file");
        mContext.registerReceiver(mReceiver, filter);
    }

    @Override
    public void onTerminate() {
        super.onTerminate();
        mContext.unregisterReceiver(mReceiver);
    }

    public static void registerMountActionListener(MountActionListener lis) {
        if (!mListeners.contains(lis)) {
            mListeners.add(lis);
        }
    }

    public static void unRegisterMountActionListener(MountActionListener lis) {
        if (mListeners.contains(lis)) {
            mListeners.remove(lis);
        }
    }

    public interface MountActionListener {
        void onReceive(String volumePath, boolean mount);
    }

    public static Context getAppContext() {
        return mContext;
    }
}
